package com.itheima.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private BookDao bookDao;

    @Test
    void testgetById() {
        System.out.println(bookDao.selectById(1));
    }
    @Test
    void testSave() {
        Book book = new Book();
        book.setType("测试数据");
        book.setName("2222");
        book.setDescription("测试数据111");
        bookDao.insert(book);
    }
    @Test
    void testDelete() {
        bookDao.deleteById(20);
    }
    @Test
    void testUpdate() {
        Book book = new Book();
        book.setType("nihao ");
        book.setName("woho");
        book.setId(19);
        bookDao.updateById(book);
    }
    @Test
    void testGetAll() {
        System.out.println(bookDao.selectList(null));
    }
//    分页查询
    @Test
    void testGetPage() {
        IPage page=new Page(1,5);
        bookDao.selectPage(page,null);

        System.out.println(page.getPages());
        System.out.println(page.getCurrent());
        System.out.println(page.getRecords());
        System.out.println(page.getTotal());
        System.out.println(page.getSize());
    }
//    按条件查询,查询功能
    @Test
    void testBy() {
        QueryWrapper<Book> bqw = new QueryWrapper<>();
        bqw.like("name","spring");
        bookDao.selectList(bqw);
    }

    @Test
    void testBy2() {
        String name="spring";
        LambdaQueryWrapper<Book> bqw = new LambdaQueryWrapper<>();
        bqw.like(name!=null,Book::getName,name);
        bookDao.selectList(bqw);
    }
}
